<?php
/**
 * @author Martin Pernica
 * @copyright 2008
 */

echo '<br /><h2><a href="index.php?p=components&name=poll">'.Lang::getBackGlobal('back').'</a></h2><br />';

if (!empty($_POST['question'])) {

    $question = Registry::get('db')->real_escape_string($_POST['question']);
    $answers = array();
    for ($i = 1; !empty($_POST['answer-' . $i]); $i++) {
        $answers[$i] = $_POST['answer-' . $i];
    }
    $active = intval($_POST['active']);
    $answers = serialize($answers);
    $query = Registry::get('db')->query("UPDATE {prefix}poll SET question = '" . $question . "', answers = '" . $answers .
        "', active = '" . $active . "' WHERE id = '" . intval($_GET['id']) . "'");
    $log->write(8, 'com_poll', 'NOW()', 'User edited poll #'.intval($_GET['id']), $_SESSION['id']);
    
    messageBox( Lang::getBack('com_poll', 'edited_done') );
}

$query = Registry::get('db')->query("SELECT * FROM {prefix}poll WHERE id = '" . intval($_GET['id']) . "'");
$data = $query->fetch_array();
$active = ($data['active'] == 1) ? 1 : 0;


$form = new Form;
$form->addTextbox('question', Lang::getBack('com_custom', 'question'), stripslashes($data['question']));

$answers = unserialize($data['answers']);
$i = 1;

foreach ($answers as $key => $val) {
    $form->addTextbox('answer-' . $key, Lang::getBack('com_poll', 'answer_no') . ' ' . $i, $val);
    $i++;
}

$form->addCheckbox('active', Lang::getBack('com_poll', 'active'), 1, $active);
$form->addSubmit('submit_edit', Lang::getBackGlobal('submit'));

echo $form;

?>